package com.techstar.ats.codec;

import java.time.LocalDateTime;

import io.netty.buffer.ByteBuf;

public enum DateTimeCodec implements PrimitiveCodec<LocalDateTime> {
	INSTANCE;

	public LocalDateTime get(ByteBuf buf) {
		int year = buf.readByte() * 100;
		year += buf.readByte();
		int month = buf.readUnsignedByte();
		int day = buf.readUnsignedByte();
		int hour = buf.readUnsignedByte();
		int minute = buf.readUnsignedByte();
		int second = buf.readUnsignedByte();

		return LocalDateTime.of(year, month, day, hour, minute, second);
	}

	public void encode(ByteBuf buf, LocalDateTime value) {
		buf.writeByte(value.getYear() / 100);
		buf.writeByte(value.getYear() % 100);
		buf.writeByte(value.getMonthValue());
		buf.writeByte(value.getDayOfMonth());
		buf.writeByte(value.getHour());
		buf.writeByte(value.getMinute());
		buf.writeByte(value.getSecond());
	}
}
